package com.netposa.datawarehouse.aggregator.client.search;

import com.google.common.collect.Lists;
import com.netposa.appwebres.common.WebResInfo;
import com.netposa.datawarehouse.aggregator.search.ResourceParam;
import com.netposa.datawarehouse.aggregator.search.ResourceResult;
import com.netposa.datawarehouse.aggregator.search.ResourceSearch;
import com.netposa.datawarehouse.aggregator.web.model.DefaultCondition;
import com.netposa.library.datawarehouse.aggregate.helper.ResourceDefined.ResourceInnerFiled;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;

public abstract class AbstractResourceSearch implements ResourceSearch {
   private static final Logger log = LoggerFactory.getLogger(AbstractResourceSearch.class);

   public List<ResourceResult> search(List<ResourceParam> resourceParams) throws Exception {
      throw new Exception("current method did not implemented .....");
   }

   public List<ResourceResult> selectInParams(DefaultCondition defaultCondition) throws Exception {
      throw new Exception("current method did not implemented.....");
   }

   public Integer count(DefaultCondition defaultCondition) throws Exception {
      throw new Exception("current method : [count] did not implemented.....");
   }

   public List<ResourceResult> selectInParams2(RequestMethod requestMethod, String serviceName, String path, String typeName, String requestBody) throws Exception {
      throw new Exception("current method : [selectInParams2] did not implemented.....");
   }

   public Integer count(WebResInfo webResInfo) throws Exception {
      if (webResInfo.getCode().equalsIgnoreCase("200") && webResInfo.getData() != null) {
         Double count = Double.valueOf(String.valueOf(webResInfo.getData()));
         log.info("=======================>count={}", count);
         return count.intValue();
      } else {
         log.error("Request server for count failed {} = {} .", webResInfo.getCode(), webResInfo.getMessage());
         return 0;
      }
   }

   public List<ResourceResult> rebuild(WebResInfo webResInfo, String type) {
      if (webResInfo.getCode().equalsIgnoreCase("200") && webResInfo.getData() != null) {
         Map<String,Object> resultData = (Map)webResInfo.getData();
         List<Map<String,Object>> list = (List)resultData.get("list");
         return Lists.transform(list, (input) -> {
            ResourceResult result = new ResourceResult();
            result.setData(input);
            result.getData().put(ResourceInnerFiled._TYPE.getFiled(), type);
            result.getData().put(ResourceInnerFiled._RECORD_ID.getFiled(), result.getData().get("recordId"));
            result.getData().put(ResourceInnerFiled._ABS_TIME.getFiled(), result.getData().get("absTime"));
            result.getData().put(ResourceInnerFiled.SOURCE.getFiled(), result.getData().get("source"));
            result.addRequestId(type, Objects.toString(resultData.get("requestId")));
            return result;
         });
      } else {
         log.error("Request {} server failed {} = {} .", type, webResInfo.getCode(), webResInfo.getMessage());
         return Lists.newArrayList();
      }
   }
}
